//
//  WebViewViewController.m
//  Init
//
//  Created by zhaoshijie on 20/01/2018.
//  Copyright © 2018 zhaoshijie. All rights reserved.
//

#import "EBMWebViewViewController.h"
//#import "SJDataManageTool.h"
//#import "EBMWechatManagerSingle.h"
//#import "EBMJumpClass.h"



@interface EBMWebViewViewController ()

@end

@implementation EBMWebViewViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //  创建控件    webView
    [self createViewInWebView];
    
}


//  创建控件    webView
- (void)createViewInWebView
{
    
    //  配置交互
    [self.userContent addScriptMessageHandler:self name:@"getLoginUser"];
    [self.userContent addScriptMessageHandler:self name:@"callMPShare"];
    [self.userContent addScriptMessageHandler:self name:@"userLogin"];
    [self.userContent addScriptMessageHandler:self name:@"redirectHomeTab"];
    

}




#pragma mark ----------初始化网络请求----------
//  初始化网络请求
- (void)requestData
{
    
}

//  网络请求    用户信息
- (void)requestDataInUserInfoWithSuccessBlock:(void(^)(id result))successBlock
{
    /*
    NSString *urlString = Myself_member_queryMemberById;
        

    [EBMRequest GETRequestTakeDataWithUrlString:urlString headerDic:nil parameters:nil loadView:self.view tableView:nil successBlock:^(id result) {
        
        
        
        if(successBlock)
        {
            successBlock(result);
        }
        
    } serverErrorBlock:nil failBlock:nil];
     */
    
    
}


#pragma mark --------------方法--------------

//  加载网页
- (void)loadWebView
{
    
    //  设置网页的请求头
    
    /*
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.urlString]];
    
    GTBUserSingle *userSingle = [GTBUserSingle shareUserSingle];
    
    //  设置网页请求头
    if([userSingle isLogin])
    {
  
        [request addValue:userSingle.token forHTTPHeaderField:@"token"];
        [request addValue:userSingle.mobile forHTTPHeaderField:@"mobile"];
        
        NSString *version = [SJAppInformationTool gainApplicationVersion];
        
        [request addValue:version forHTTPHeaderField:@"version"];
        
    }
     
    
    //  加载网页
    [self.webView loadRequest:request];
     */
    
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.urlString]];
    
    //  加载网页
    [self.webView loadRequest:request];
    
}


/// 获取用户信息
- (void)getUserInfo
{

    [self requestDataInUserInfoWithSuccessBlock:^(id result) {
        
//        NSString *parmString = [SJDataManageTool jsonClassConvertToJosnStringWithJsonClass:result];

        //  OC掉用JS方法        JS注入
//        NSString *message = [NSString stringWithFormat:@"onNativeLoginSuccess(%@)",parmString];
//        [self.webView evaluateJavaScript:message completionHandler:nil];
        
    }];
    
}



#pragma mark --------------响应方法--------------

//  点击关闭
- (void)closeItemClick:(UIBarButtonItem *)item
{
    //  关闭
    [self.navigationController popViewControllerAnimated:YES];
}

//  返回上一层
- (void)backButtonClick:(UIButton *)sender
{
    if (self.webView.canGoBack)
    {
        //  返回上一层
        [self.webView goBack];
        
    }else
    {
        [self.navigationController popViewControllerAnimated:YES];
        
        if(self.operationBlock)
        {
            self.operationBlock();
        }
    }
}


#pragma mark ----------WKWebView 代理方法----------


// 如果不添加这个，那么wkwebview跳转不了AppStore
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    if ([webView.URL.absoluteString hasPrefix:@"https://itunes.apple.com"])
    {
        
        [[UIApplication sharedApplication]openURL:navigationAction.request.URL options:@{} completionHandler:nil];
        
        decisionHandler(WKNavigationActionPolicyCancel);
        
    }
    else
    {
        decisionHandler(WKNavigationActionPolicyAllow);
    }
    
    
    //  网页跳转地址拦截
//    NSString *requestUrl = [navigationAction.request.URL.absoluteString stringByRemovingPercentEncoding];
    
    
 
//    if ([[requestUrl lowercaseString] isEqualToString:[@"gtb://APP_RECHARGE" lowercaseString]]) {
//
//        //  进入充值界面
//        [self enterRechargeVCWithMoney:nil];
//
//    }
    
}


// WKScriptMessageHandler       js掉用OC的方法
- (void)userContentController:(WKUserContentController *)userContentController
      didReceiveScriptMessage:(WKScriptMessage *)message
{
    
    
//    if([message.name isEqualToString:@"login"])
//    {
//        /*
//        if([[GTBUserSingle shareUserSingle] isLogin])
//        {
//            //  OC掉用JS方法        JS注入
//            NSString *message = [NSString stringWithFormat:@"webPageParam(%@)",[GTBUserSingle shareUserSingle].mobile];
//            [self.webView evaluateJavaScript:message completionHandler:nil];
//        }
//        else
//        {
//            [self enterLogin];
//        }
//         */
//    }
//    else if([message.name isEqualToString:@"getLoginUser"] || [message.name isEqualToString:@"userLogin"])
//    {
//        
//        
//        //  当body为0的时候不需要登录。1的时候需要登录
//        id body = message.body;
//        
//        
//        
//        //  获取用户信息，用户登录     都进行登录+传递用户信息
//        
//        if(KUserSingeIsLogin)
//        {
//            
//            //  已登录
//            
//            /// 获取用户信息
//            [self getUserInfo];
//            
//        }
//        else
//        {
//            
//            if([body isKindOfClass:[NSNumber class]] && [body integerValue] == 0)
//            {
//                //  不需要登录
//                return;
//            }
//            
//            [EBMLoginMethod naviagtionEnterLoginVCWithVC:self loginSuccessBlock:^{
//                
//               /// 获取用户信息
//               [self getUserInfo];
//                
//            }];
//            
//        }
//        
//    }
//    else if([message.name isEqualToString:@"callMPShare"])
//    {
//        
//        NSDictionary *shareParameter_dic = message.body;
//        
//        if([shareParameter_dic isKindOfClass:[NSDictionary class]])
//        {
//            
//            //  0：正式      1：ebondtest2
//            NSInteger channel = [shareParameter_dic[@"channel"] integerValue];
//            NSInteger type = [shareParameter_dic[@"type"] integerValue];
//            
//            NSString *description = shareParameter_dic[@"description"];
//            
//            NSString *imgUrl = shareParameter_dic[@"imgUrl"];
//            NSData * imgUrlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgUrl]];
//            
//            NSString *path = shareParameter_dic[@"path"];
//            
//            NSString *title = shareParameter_dic[@"title"];
//            
//            [EBMWechatManagerSingle sendAuthRequestWithTitle:title
//                                                 description:description
//                                                        path:path
//                                                 hdImageData:imgUrlData
//                                                   thumbData:imgUrlData
//                                                     channel:channel
//                                                        type:type
//                                                  controller:self
//                                           shareSuccessBlock:^{
//                
//            }];
//            
//        }
//        
//    }
//    else if([message.name isEqualToString:@"redirectHomeTab"])
//    {
//        NSArray *jump_array = message.body;
//        
//        if(jump_array.count == 2)
//        {
//            NSInteger parm0 = [jump_array[0] integerValue];
//            NSInteger parm1 = [jump_array[1] integerValue];
//            
//            if(parm0 == 1 && parm1>=0 && parm1<=3)
//            {
//                //  债券列表
//                
//                [EBMJumpClass jumpInBondHomeWithType:parm1+1];
//            }
//            
//            
//        }
//        
//    }
    
}




@end
